package cn.dliyy.base.service;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import cn.dliyy.base.bean.CountryRegion;
import cn.dliyy.file.CsvUtil;
import cn.dliyy.warehouse.dao.EntityDao;

@Service
public class CountryRegionService {

	@Resource
	private EntityDao entityDao;

	public void save(CountryRegion cg) {
		entityDao.save(cg);
	}

	public void importCountryRegions(String fileName) throws IOException {
		CsvUtil cu = new CsvUtil(fileName);
		CountryRegion cg = new CountryRegion();
		for (int i = 0; i < cu.getRowNum(); i++) {
			cg.setCountryCode(cu.getString(i, 0));
			cg.setRegionCode(cu.getString(i, 1));
			cg.setRegionName(cu.getString(i, 2));
			entityDao.save(cg);
		}
		cu.CsvClose();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CountryRegionService crs = (CountryRegionService)appContext.getBean("countryRegionService");
		String fileName = "E:/鼎立/国内地区代码.csv";
		try {
			crs.importCountryRegions(fileName);
		} catch (IOException e) {
			System.out.println("error: " + fileName);
			e.printStackTrace();
		}
		System.out.println("it's end");
	}

}
